/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui;

import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.spotimage.catalog.client.ui.component.MyWindow;
import com.spotimage.catalog.client.ui.listgrid.SceneListGrid;
import com.spotimage.catalog.client.ui.listgrid.SceneListGridRecord;
import com.spotimage.catalog.client.util.SceneTypes;

public class SelectionExportWindow extends MyWindow {

	public SelectionExportWindow(SceneListGrid listGrid) {
		super(false, false, false);
		setTitle("Selection export");
		setWidth(900);
		setHeight("80%");
		
		VLayout layout = new VLayout();
		layout.setPadding(new Integer(5));
		layout.setBackgroundColor(Ui.BACKGROUND_COLOR);
		layout.setWidth100();
		layout.setHeight100();
		
		Label label = new Label("Copy the CSV formatted text below in the clipboard (Ctrl+A, then Ctrl+C) :");
		label.setWidth100();
		label.setHeight(20);
		label.setWrap(false);
		layout.addMember(label);
		
		DynamicForm dynamicForm = new DynamicForm();
		dynamicForm.setWidth100();
		dynamicForm.setHeight100();
		TextAreaItem area = new TextAreaItem();
		area.setWidth(870);
		area.setHeight("100%");
		area.setValue(getData(listGrid));
		area.setShowTitle(false);
		dynamicForm.setItems(area);
		layout.addMember(dynamicForm);
		
		addItem(layout);
		
		show();
		area.focusInItem();
	}

	private String getData(SceneListGrid listGrid) {
		String data = "Scene;Satellite;Type;Acquisition date;Cloud cover %;Snow cover %;Shift;Minimum shift;Maximum shift;" +
				"Upper left latitude;Upper left longitude;Upper right latitude;Upper right longitude;" +
				"Lower left latitude;Lower left longitude;Lower right latitude;Lower right longitude;" +
				"Archiving station";
		RecordList recordList = listGrid.getRecordList();
		for (int i = 0; i < recordList.getLength(); i++) {
			SceneListGridRecord record = (SceneListGridRecord) recordList.get(i);
			data += "\n";
			data += record.getId() + ";"; 
			data += record.getSatellite() + ";"; 
			data += SceneTypes.get(record.getId()) + ";"; 
			data += record.getAcquisitionDateString() + ";"; 
			data += record.getCloudCoverPercentage() + ";"; 
			data += record.getSnowCoverPercentage() + ";"; 
			data += record.getShift() + ";"; 
			data += record.getMinShift() + ";"; 
			data += record.getMaxShift() + ";"; 
			data += record.getUpperLeft().getLatitude() + ";"; 
			data += record.getUpperLeft().getLongitude() + ";"; 
			data += record.getUpperRight().getLatitude() + ";"; 
			data += record.getUpperRight().getLongitude() + ";"; 
			data += record.getLowerLeft().getLatitude() + ";"; 
			data += record.getLowerLeft().getLongitude() + ";"; 
			data += record.getLowerRight().getLatitude() + ";"; 
			data += record.getLowerRight().getLongitude() + ";"; 
			data += record.getArchivingStation(); 
		}
        return data;
	}

}
